package com.cqw.commontest.lottery;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-08 17:47
 * @Description:
 */
public class TimeLottery {


    //总的奖项数据
    public static List<RewardConfig> rewardConfigList = new ArrayList<>();

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    static {
        try {
            rewardConfigList.add(new RewardConfig(1L, "小米手机1", 1, 1, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigList.add(new RewardConfig(2L, "小米手机2", 2, 2, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigList.add(new RewardConfig(3L, "小米手机3", 3, 3, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigList.add(new RewardConfig(4L, "小米手机4", 4, 2, simpleDateFormat.parse("2021-04-09 13:47:00")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws ParseException {
        long now = System.currentTimeMillis();
        long startTime = simpleDateFormat.parse("2021-04-09 10:00:00").getTime();
        long endTime = simpleDateFormat.parse("2021-04-10 12:00:00").getTime();

        Integer totalAmount = 0; //总的奖品数
        Integer totalBalance = 0;   //总的剩余奖品数
        for (RewardConfig rewardConfig : rewardConfigList) {
            totalAmount += rewardConfig.getAmount();
            totalBalance += rewardConfig.getBalance();
        }

        RewardConfig award = rewardConfigList.get(3);

        //如果当前奖品没被抽走，出奖时间点要求不变，所以需要一个种子。我们在奖品类上保存该奖品上一次被抽中时间
        long lastUpdateTime = award.getLastUpdateTime().getTime();
        //时间片段
        long deltaTime = (endTime - startTime) / totalAmount;
        //使用lastUpdateTime作为随机种子，保证下一个奖品的随机时间对每一个抽奖者都一样
        Random random = new Random(lastUpdateTime);
        //计算下一个奖品释放时间,开始时间+抽中奖品已经消耗的时间+随机数%每个时间片段的时间
        long time = Math.abs(random.nextLong()) % deltaTime;
        System.out.println(time);
        long releaseTime = startTime + (totalAmount - totalBalance) * deltaTime + Math.abs(random.nextLong()) % deltaTime;

        //未达到下一奖品释放时间,刷成未中奖
        if (now < releaseTime) {
            System.out.println("未中奖");
        }
    }


}
